﻿namespace Chunker.Inpe.Data {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Chunker.Inpe.Data.SqlServer;
    using System.Data;

    public static class ImageController {
        public static int SaveImage(string filepath, int idConfiguration) {
            using (var entities = new InpeEntitiesSqlServer()) {

                string fileName = new FileInfo(filepath).Name;

                Image image = entities.Image.FirstOrDefault(x => x.FileName == fileName && x.Configuration.IdConfiguration == idConfiguration);
                if (image != null) {
                    return image.ImageId;
                }

                image = new Image();
                image.FileName = fileName;
                image.Date = DateTime.Now;//GetDate(filepath);
                image.ContentHash = Util.GetHash(filepath);
                image.Projection = "Rectangular";
                image.Resolution = "Low";
                image.Sector = "South America";
                image.Source = "Goes 12";
                image.Type = GetImageType(filepath);
                image.Completed = false;
                image.ConfigurationReference.EntityKey = new EntityKey("InpeEntitiesSqlServer.Configuration", "IdConfiguration", idConfiguration);

                entities.AddToImage(image);
                entities.SaveChanges();

                return image.ImageId;
            }
        }

        public static bool FileCompleted(string filePath, int idConfiguration) {
            string fileName = new FileInfo(filePath).Name;
            using (var entities = new InpeEntitiesSqlServer()) {
                Image image = entities.Image.FirstOrDefault(x => x.FileName.Equals(fileName) && x.Configuration.IdConfiguration == idConfiguration);
                return image == null ? false : image.Completed;
            }
        }

        public static void SetCompleted(string filePath, int idConfiguration) {
            string fileName = new FileInfo(filePath).Name;
            using (var entities = new InpeEntitiesSqlServer()) {
                Image image = entities.Image.FirstOrDefault(x => x.FileName.Equals(fileName) && x.Configuration.IdConfiguration == idConfiguration);
                image.Completed = true;
                entities.SaveChanges();
            }
        }

        private static DateTime GetDate(string filename) {
            string strDate = filename.Substring(filename.LastIndexOf("_") + 1, 12);
            string strYear = strDate.Substring(0, 4);
            string strMonth = strDate.Substring(4, 2);
            string strDay = strDate.Substring(6, 2);
            string strHour = strDate.Substring(8, 2);
            string strMinute = strDate.Substring(10, 2);

            DateTime date = new DateTime(int.Parse(strYear), int.Parse(strMonth), int.Parse(strDay), int.Parse(strHour), int.Parse(strMinute), 0);
            return date;
        }

        private static string GetImageType(string filename) {
            if (filename.Contains("infra")) {
                return "Infra";
            }
            if (filename.Contains("vapor")) {
                return "Steam";
            }
            if (filename.Contains("col")) {
                return "Color";
            }
            if (filename.Contains("realcada")) {
                return "Enhanced";
            }

            return "Composed";
        }
    }
}